<template>
  <div>
    <van-address-list
      :list="list"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
      @select="select($event)"
    />

    <van-empty v-if="list.length == 0" description="暂无数据" />
  </div>
</template>

<script>
import { reqAddressList } from "../../http/http";
export default {
  data() {
    return {
      // 1.初始化数据
      list: [
        /*{
          id: "1",
          name: "张三",
          tel: "13000000000",
          address: "浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室",
          isDefault: true,
        },*/
      ],
    };
  },
  mounted() {
    // 2.获取地址数据
    reqAddressList({
      uid: JSON.parse(localStorage.getItem("userInfo")).uid,
    }).then((res) => {
      if (res.data.code == 200) {
        let arr = res.data.list ? res.data.list : [];
        // 从后端取回来数据，将数据的字段转为UI框架需要的字段
        this.list = arr.map((item) => {
          return {
            id: item.id,
            name:item.username,
            tel:item.userphone,
            address: item.location+item.useraddress,
            isDefault: item.status===1,
          };
        });
      }
    });
  },
  methods: {
    //点了新增
    onAdd() {
      this.$router.push("/addr");
    },
    //点了编辑
    onEdit(e) {
      this.$router.push("/addr?id=" + e.id);
    },
    //选中一个
    select(e){
      this.$router.replace("/order?addressId="+e.id)
    }
  },
};
</script>

<style>
</style>